<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xforms="http://www.w3.org/2002/01/xforms">
  
  <xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
  <xsl:strip-space elements="*" />
  
  <!-- user defined action -->
  <xsl:param name="form.action" />

  <xsl:template match="*" mode="xform">
  	<xsl:variable name="action">
		<xsl:choose>
			<xsl:when test="string-length($form.action) != 0">
				<xsl:value-of select="$form.action" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="xforms:model/xforms:submission/@action" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
    <form method="{xforms:model/xforms:submission/@method}" action="{$action}">
      <table class="xforms_page">
        <xsl:apply-templates />
      </table>
    </form>
  </xsl:template>
  
  <!-- Empty template for <xforms:instance> element because the instance data is handled differently -->
  <xsl:template match="//xforms:instance" />
  
  <xsl:template name="makepath">
    <xsl:param name="node" />
    <xsl:param name="path" />
    <xsl:variable name="this" select="$node" />
    <xsl:variable name="parent" select="$node/ancestor::*[1]" />
    <xsl:choose>
        <xsl:when test="$parent!=''">
            <xsl:variable name="newpath">
                <xsl:choose>
                    <xsl:when test="name($node)='xforms:input'">
                        <xsl:value-of select="concat('[', $node/@ref, ']', $path)" />
                    </xsl:when>
                    <xsl:when test="name($node)='xforms:repeat'">
                        <xsl:copy-of select="concat('[', $node/@nodeset, '][0]', $path)" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="$path" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:variable>
            <xsl:call-template name="makepath">
              <xsl:with-param name="node" select="$parent" />
              <xsl:with-param name="path" select="$newpath" />
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$path" />
        </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="//xforms:input">
    <xsl:variable name="path">
        <xsl:call-template name="makepath">
            <xsl:with-param name="node" select="." />
            <xsl:with-param name="path" select="''" />
        </xsl:call-template>
    </xsl:variable>
    <tr>
      <td><xsl:apply-templates /></td>
      <td><input class="xforms_input" type="text" name="item{$path}"/></td>
    </tr>
  </xsl:template>
  
  <xsl:template match="//xforms:label">
    <span class="xforms_label"><xsl:value-of select="."/></span>
  </xsl:template>
  
  <xsl:template name="trigger">
    <input type="button" class="xforms_trigger" name="{@id}" value="{xforms:label}"/>
  </xsl:template>
  
  <xsl:template match="//xforms:trigger" />
  
  <xsl:template match="//xforms:group">
    <tr>
      <td colspan="2">
        <table class="xforms_group">
          <xsl:apply-templates />
          <tr>
            <td colspan="2">
              <xsl:for-each select="xforms:trigger">
                <xsl:call-template name="trigger" />
              </xsl:for-each>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </xsl:template>
  
  <xsl:template match="//xforms:submit">
    <tr>
      <td colspan="2">
        <input type="submit" class="xforms_submit" name="{@submission}" value="{xforms:label}" />
      </td>
    </tr>
  </xsl:template>
</xsl:transform>
